package com.inspur.ydt.service.impl;

import java.util.HashMap;
import java.util.Map;


//import org.apache.logging.log4j.LogManager;
//import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import com.inspur.iop.model.PostParameter;
import com.inspur.ydt.service.CompanyService;
import com.inspur.ydt.util.RequestUtils;

@Service("companyServiceImp")
public class CompanyServiceImpl implements CompanyService {

//	static Logger logger = LogManager.getLogger(CompanyServiceImpl.class.getName());
	@Override
	public Object getCompanyInfoByNameAndCode(String nameKey, String companyName, 
			String codeKey, String creditCode,
			String url, String version) {
		//企业名称和社会信用代码参数均未传入
		if (isContainNameOrCode(companyName, creditCode)) {
			//
			return "企业名称、社会信用代码应至少传入一个";
		}
		//去除空格
		companyName = companyName != null ? companyName.trim() : companyName;
		creditCode = creditCode != null ? creditCode.trim() : creditCode;
	    //只传入一个参数，且参数内容为空 或者或者两个参数内容均为空
		if (isContainNotNullNameOrCode(companyName, creditCode)) {
			return "企业名称和社会信用代码内容均为空";
		}
		Map<String, String> parametersMap = new HashMap<>();
		if (companyName != null && !"".equals(companyName)) {
			parametersMap.put(nameKey, companyName);
		}
		if (creditCode != null && !"".equals(creditCode)) {
			parametersMap.put(codeKey, creditCode);
		}
		//生成请求参数
		PostParameter[] postparams = RequestUtils.generatePostParameters(parametersMap);
 		String result = RequestUtils.requestInfo(url, version, postparams);
//		logger.info("visitResult: "+result);
		 return result;
	}

	@Override
	public Object getCompanyInfoByNameCodeAndDate(String nameKey, String companyName, String codeKey, String creditCode,
			String dateKey, String dataMonth, String url, String version) {
		//去除空格
		companyName = companyName != null ? companyName.trim() : companyName;
		creditCode = creditCode != null ? creditCode.trim() : creditCode;
		dataMonth = dataMonth != null ? dataMonth.trim() : dataMonth;
		if (dataMonth == null || "".equals(dataMonth)) {
			return "缴费年月为必填项";
		}
		//企业名称和社会信用代码参数均未传入
		if (isContainNameOrCode(companyName, creditCode)) {
			//
			return "企业名称、社会信用代码应至少传入一个";
		}
	    //只传入一个参数，且参数内容为空 或者或者两个参数内容均为空
		if (isContainNotNullNameOrCode(companyName, creditCode)) {
			return "企业名称和社会信用代码内容均为空";
		}
		Map<String, String> parametersMap = new HashMap<>();
		parametersMap.put(dateKey, dataMonth);
		if (companyName != null && !"".equals(companyName)) {
			parametersMap.put(nameKey, companyName);
		}
		if (creditCode != null && !"".equals(creditCode)) {
			parametersMap.put(codeKey, creditCode);
		}
		//生成请求参数
		PostParameter[] postparams = RequestUtils.generatePostParameters(parametersMap);
		String result = RequestUtils.requestInfo(url, version, postparams);
//		logger.info("visitResult: "+result);
		return result;
	}
	
	@Override
	public Object getCompanyInfoById(String idKey, String idValue, String url, String version) {
		//生成请求参数
		PostParameter[] postparams = RequestUtils.generatePostParameters(idKey, idValue);
		String result = RequestUtils.requestInfo(url, version, postparams);
		return result;
	}
	
	/**
	 * 是否传入了其中一项
	 * @param companyName
	 * @param creditCode
	 * @return
	 */
	private boolean isContainNameOrCode(String companyName, String creditCode) {
		return companyName == null && creditCode == null;
	}
	
	/**
	 * 是否有一项非空
	 * @param companyName
	 * @param creditCode
	 * @return
	 */
	private boolean isContainNotNullNameOrCode(String companyName, String creditCode) {
		return (companyName != null && creditCode == null && "".equals(companyName))
				|| (creditCode != null && companyName == null && "".equals(creditCode)) 
				|| (creditCode != null && "".equals(creditCode) && companyName != null && "".equals(companyName));
	}

}
